package com.example.fan.airline_reservation.fragment_class;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bin.david.form.annotation.SmartColumn;
import com.bin.david.form.annotation.SmartTable;
import com.bin.david.form.data.style.FontStyle;
import com.example.fan.airline_reservation.Model.Fight;
import com.example.fan.airline_reservation.Model.Order;
import com.example.fan.airline_reservation.Model.Temp;
import com.example.fan.airline_reservation.R;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;


public class ac_statistic extends Fragment {
    TextView total;
    com.bin.david.form.core.SmartTable table2;
    List<PInfo> Plist = new ArrayList<PInfo>();
    int flightNum=0;
    List<Fight> fightList=new ArrayList<Fight>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_ac_statistics,
                container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bmob.initialize(getActivity(), "0802cbf346296dc9fac5a477f911ba39");

        total=(TextView)getActivity().findViewById(R.id.total);
        table2=(com.bin.david.form.core.SmartTable)getActivity().findViewById(R.id.table2);

        String bq = "select * from Fight order by DATE";
        new BmobQuery<Fight>().doSQLQuery(bq,new SQLQueryListener<Fight>(){
            @Override
            public void done(BmobQueryResult<Fight> result, BmobException e) {
                if(e ==null){
                    List<Fight> list = (List<Fight>) result.getResults();
                    Message message=handler.obtainMessage();
                    message.what=0;
                    message.obj=list;
                    handler.sendMessage(message);
                }else{
                    ToastShow("错误码："+e.getErrorCode()+"，错误描述："+e.getMessage());
                }
            }
        });
    }

    @SmartTable(name="航班信息")
    public class PInfo {
        @SmartColumn(id = 1,name = "航班号")
        private String flight;
        @SmartColumn(id = 2,name = "机型")
        private String model;
        @SmartColumn(id = 3,name = "出发时间")
        private String date;
        @SmartColumn(id = 4,name = "出发地")
        private String from;
        @SmartColumn(id = 5,name = "到达时间")
        private String todate;
        @SmartColumn(id = 6,name = "目的地")
        private String to;
        @SmartColumn(id = 7,name = "乘客数量")
        private String people;
        @SmartColumn(id = 8,name = "状态")
        private String state;

        public PInfo(String flight, String model, String date, String from, String todate, String to, String people, int state) {
            this.flight = flight;
            this.model = model;
            this.date = date;
            this.from = from;
            this.todate = todate;
            this.to = to;
            this.people = people;
            switch (state){
                case 0:this.state="准点";break;
                case 1:this.state="延误";break;
                case 2:this.state="取消";break;
            };
        }

    }

    public void ToastShow(String message){
        Toast.makeText(getActivity(),message,Toast.LENGTH_SHORT).show();
    }

    private Handler handler=new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                case 0:
                    fightList=(List<Fight>) msg.obj;
                    show();
                    break;
            }
        }
    };
    public void show(){
        for (int i=0;i<fightList.size();i++){
            Fight f = fightList.get(i);
            PInfo p =new PInfo(f.getFLIGHT(),
                    f.getAIRCRAFT_MODEL(),
                    f.getDATE().toString(),
                    f.getFROM()+"-"+f.getFROMNAME(),
                    f.getTODATE().toString(),
                    f.getTO()+"-"+f.getTONAME(),
                    f.getCurrentPeole()+"",
                    f.getCurrentState());
            Plist.add(p);
            flightNum++;
        }
        total.setText("累计共有"+flightNum+"架次航班");
        table2.setData(Plist);
        table2.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
    }
}
